﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Gift.DAL
{
    /// <summary>
    /// 数据访问类tbOrderCompain。
    /// </summary>
    public class DALOrderCompain
    {
        public DALOrderCompain()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbOrderCompain");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Gift.Model.tbOrderCompain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbOrderCompain(");
            strSql.Append("OrderCode,CompainId,Content,Process,AddDate,ProcessDate)");
            strSql.Append(" values (");
            strSql.Append("@OrderCode,@CompainId,@Content,@Process,@AddDate,@ProcessDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@CompainId", SqlDbType.Int,4),
					new SqlParameter("@Content", SqlDbType.NVarChar,500),
					new SqlParameter("@Process", SqlDbType.NVarChar,500),
                    new SqlParameter("@AddDate", SqlDbType.DateTime),
                    new SqlParameter("@ProcessDate", SqlDbType.DateTime)
                                        
                                        };
            parameters[0].Value = model.OrderCode;
            parameters[1].Value = model.CompainId;
            parameters[2].Value = model.Content;
            parameters[3].Value = model.Process;
            parameters[4].Value =  model.AddDate == DateTime.Parse("0001-01-01") ? DateTime.Parse("1900-01-01") : model.AddDate;
            parameters[5].Value =  model.ProcessDate == DateTime.Parse("0001-01-01") ? DateTime.Parse("1900-01-01") : model.ProcessDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Gift.Model.tbOrderCompain model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbOrderCompain set ");
            strSql.Append("OrderCode=@OrderCode,");
            strSql.Append("CompainId=@CompainId,");
            strSql.Append("Content=@Content,");
            strSql.Append("Process=@Process,");
            strSql.Append("AddDate=@AddDate,");
            strSql.Append("ProcessDate=@ProcessDate");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@OrderCode", SqlDbType.VarChar,50),
					new SqlParameter("@CompainId", SqlDbType.Int,4),
					new SqlParameter("@Content", SqlDbType.NVarChar,500),
					new SqlParameter("@Process", SqlDbType.NVarChar,500),
                         new SqlParameter("@AddDate", SqlDbType.DateTime),
                    new SqlParameter("@ProcessDate", SqlDbType.DateTime)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.OrderCode;
            parameters[2].Value = model.CompainId;
            parameters[3].Value = model.Content;
            parameters[4].Value = model.Process;
            parameters[5].Value = model.AddDate;
            parameters[6].Value = model.ProcessDate == DateTime.Parse("0001-01-01") ? DateTime.Parse("1900-01-01") : model.ProcessDate;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete tbOrderCompain ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        public DataSet GetOrderCompainList(string sOrderCode, int iCompainId, int iProcessd)
        {
            string sSQL = string.Empty;
            sSQL = "select Id,(select top 1 orderid from DB_Order where ordercode = tbOrderCompain.OrderCode) as orderid, OrderCode,(select CompainName from tbCompain where CompainId = tbOrderCompain.CompainId) as CompainName,CompainId,[Content],Process,AddDate,ProcessDate From tbOrderCompain"
                   + " where OrderCode like '%'+ @OrderCode +'%' ";
            if (iCompainId != 0)
            {
                sSQL = sSQL + "  and CompainId=@CompainId ";
            }
            if (iProcessd == 1)
            {
                sSQL = sSQL + " and isnull(Process,'') != '' ";
            }
            else
            {
                sSQL = sSQL + " and isnull(Process,'') = '' ";
            }
            sSQL = sSQL + " order by AddDate desc";
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.NVarChar,50),
                                        new SqlParameter("@CompainId", SqlDbType.Int,4),
                                        new SqlParameter("@Processd", SqlDbType.Int,4)};
            parameters[0].Value = sOrderCode;
            parameters[1].Value = iCompainId;
            parameters[2].Value = iProcessd;

           return DbHelperSQL.Query(sSQL, parameters);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbOrderCompain GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,CompainId,Content,Process,AddDate,ProcessDate from tbOrderCompain ");
            strSql.Append(" where Id=@Id ");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Gift.Model.tbOrderCompain model = new Gift.Model.tbOrderCompain();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.OrderCode = ds.Tables[0].Rows[0]["OrderCode"].ToString();
                if (ds.Tables[0].Rows[0]["CompainId"].ToString() != "")
                {
                    model.CompainId = int.Parse(ds.Tables[0].Rows[0]["CompainId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProcessDate"].ToString() != "")
                {
                    model.ProcessDate = DateTime.Parse(ds.Tables[0].Rows[0]["ProcessDate"].ToString());
                }
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                model.Process = ds.Tables[0].Rows[0]["Process"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Gift.Model.tbOrderCompain GetModelByOrderCode(string sOrderCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,CompainId,Content,Process,AddDate,ProcessDate from tbOrderCompain ");
            strSql.Append(" where OrderCode=@OrderCode ");
            SqlParameter[] parameters = {
					new SqlParameter("@OrderCode", SqlDbType.VarChar,50)};
            parameters[0].Value = sOrderCode;

            Gift.Model.tbOrderCompain model = new Gift.Model.tbOrderCompain();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                model.OrderCode = ds.Tables[0].Rows[0]["OrderCode"].ToString();
                if (ds.Tables[0].Rows[0]["CompainId"].ToString() != "")
                {
                    model.CompainId = int.Parse(ds.Tables[0].Rows[0]["CompainId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ProcessDate"].ToString() != "")
                {
                    model.ProcessDate = DateTime.Parse(ds.Tables[0].Rows[0]["ProcessDate"].ToString());
                }
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                model.Process = ds.Tables[0].Rows[0]["Process"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,OrderCode,CompainId,Content,Process ");
            strSql.Append(" FROM tbOrderCompain ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbOrderCompain";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}

